package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.param.AlbumAddNewParam;
import cn.tedu.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.security.LoginPrincipal;
import cn.tedu.csmall.product.pojo.vo.PictureStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.Ignore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/albums")
@Api(tags = "04. 相册管理模块")
@Validated
public class AlbumController {

    @Autowired
    private IAlbumService albumService;

    // http://localhost:9180/albums/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult addNewAlbum(@Valid /*@RequestBody*/ AlbumAddNewParam albumAddNewParam) {
        log.debug("开始处理【添加相册】请求，参数：{}", albumAddNewParam);
        albumService.addNew(albumAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/albums/9529/delete
    @PostMapping("/{id:^\\d+$}/delete")
    @ApiOperation("根据ID删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "根据ID删除相册失败，请提交合法的ID值！")
          /*@RequestParam*/  @PathVariable Long id) {
        log.debug("开始处理【根据ID删除相册】请求，参数：{}", id);
        albumService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/albums/9529/update
    @PostMapping("/{id:^\\d+$}/update")
    @ApiOperation("修改相册详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@Range(min = 1, message = "请提交有效的相册ID值！")
                                     @PathVariable    Long id,
                                     @Valid AlbumUpdateInfoParam albumUpdateInfoParam) {
        log.debug("开始处理【修改相册详情】请求，ID:{},参数：{}", id,albumUpdateInfoParam);
        albumService.updateInfoById(id, albumUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/albums/9528
    @GetMapping("/{id:^\\d+$}")
    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult getAlbumStandardById(@Range(min = 1, message = "根据ID查询相册详情失败，请提交合法的ID值！")
                             @PathVariable Long id) {
        log.debug("开始处理【根据ID查询相册详情】请求，ID:{}", id);
        AlbumStandardVO query = albumService.getAlbumStandardById(id);
        return JsonResult.ok(query);
    }

    // http://localhost:9180/albums
    // http://localhost:9180/albums?queryType=all
    @GetMapping("")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(@Range(min = 1, message = "查询相册列表失败，请提供正确的页码值！") Integer page,
                           @Ignore @AuthenticationPrincipal LoginPrincipal loginPrincipal,String queryType) {
        log.debug("开始处理【查询相册列表】请求，页码：{}", page);
        log.debug("当事人：{}", loginPrincipal);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<AlbumListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData = albumService.list(page,Integer.MAX_VALUE);
        }else {
            pageData = albumService.list(page);
        }
        return JsonResult.ok(pageData);
    }

    // http://localhost:9180/albums/list/{albumId}
    @GetMapping("/list/{albumId:^\\d+$}")
    @ApiOperation("根据相册id查询图片列表")
    @ApiOperationSupport(order = 430)
    public JsonResult list(@Range(min = 1, message = "查询相册列表失败，请输入合法的相册ID！")
                           @PathVariable Long albumId, Integer pageNo, Integer pageSize) {
        log.debug("开始处理【根据相册id查询图片列表】的请求，相册id：{}，页码：{}，每页数据量：{}", albumId, pageNo, pageSize);
        pageNo = 1;
        pageSize = Integer.MAX_VALUE;
        PageData<PictureStandardVO> pageData = albumService.listByAlbumId(albumId, pageNo, pageSize);
        return JsonResult.ok(pageData);
    }
}
